package cc.zy.base.hr.service.impl;

import cc.zy.base.common.entity.FebsConstant;
import cc.zy.base.common.entity.QueryRequest;
import cc.zy.base.common.utils.SortUtil;
import cc.zy.base.hr.entity.Course;
import cc.zy.base.hr.mapper.CourseMapper;
import cc.zy.base.hr.service.ICourseService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 员工
 * Service
 *
 * @author Jiangjinlin
 * @date 2021-01-18 10:51:13
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Resource
    private final CourseMapper courseMapper;

    @Override
    public IPage<Course> findCourses(QueryRequest request, Course course) {
        
        Page<Course> page = new Page<>(request.getPageNum(), request.getPageSize());
        
        page.setSearchCount(false);
        page.setTotal(baseMapper.countCourseDetail(course));
        
        SortUtil.handlePageSort(request, page, "id", FebsConstant.ORDER_ASC, false);
        
        return baseMapper.findCourseDetailPage(page, course);
    }

    @Override
    public Course findById(Integer id) {
        return baseMapper.findById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteCourses(String[] courseIds) {
        
        List<String> list = Arrays.asList(courseIds);
        // 删除用户
        this.removeByIds(list);
    }

    @Override
    public List<Course> findCollageListNotPage(Course course) {
        
        List<Course> collageListNotPage = baseMapper.findCollageListNotPage(course);
        
        return collageListNotPage;
    }

    @Override
    public Course findCourseDetailList(Integer id) {
        
        Course param = new Course();
        
        param.setId(id);
        
        List<Course> course = baseMapper.findCourseDetail(param);
        
        return CollectionUtils.isNotEmpty(course) ? course.get(0) : null;
    }

    @Override
    public List<Course> findCourses(Course course) {
        
        LambdaQueryWrapper<Course> queryWrapper = new LambdaQueryWrapper<>();

        return baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createCourse(Course course) {
        
        course.setCreateDate(new Date());
        course.setLastModifyDate(new Date());
        
        this.save(course);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCourse(Course course) {
        this.saveOrUpdate(course);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteCourse(Course course) {
        
        LambdaQueryWrapper<Course> wrapper = new LambdaQueryWrapper<>();

        this.remove(wrapper);
    }



   

   

    

   

    
    

    @Override
    public Boolean isRepetitive(String tagName) {
        
        String repetitive = courseMapper.isRepetitive(tagName);
        
        Boolean flag = true;
        if (repetitive != null) {
            flag = false;
        }
        return flag;
    }

 



    @Override
    public Integer findMaxId() {
        return courseMapper.findMaxId();
    }
}